﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI;
using 纸片人.Shaders;
using 纸片人.win2dEffectControl.DataClass.PixelShaderEffectDataClass;

namespace 纸片人.win2dEffectControl.DataClass
{
    public class ColorReplacementEffectData : PixelShaderEffectData
    {




        private float threshold;
        private Color targetColor = Colors.White;
        private Color sourceColor = Colors.White;


        #region  属性



        /// <summary>
        /// 临界点 0-1
        /// </summary>
        public float Threshold { get => threshold; set { threshold = value; OnPropertyChanged(); SetIsChanges(); } }
        /// <summary>
        /// 替换后的颜色
        /// </summary>
        public Color TargetColor { get => targetColor; set { targetColor = value; OnPropertyChanged(); SetIsChanges(); } }
        /// <summary>
        /// 替换前的颜色
        /// </summary>
        public Color SourceColor { get => sourceColor; set { sourceColor = value; OnPropertyChanged(); SetIsChanges(); } }

        public override ShaderType shaderShaderPath { get; } = ShaderType.ColorReplacement;


        #endregion


        public override  ICanvasImage GetEffect(CanvasDevice device, bool compression)
        {
            var source = GetSourceEffect(device, compression);
            if (source == null)
            {
                return null;
            }

            if (shaderCodeBytes == null)
                return null;

            PixelShaderEffect effect = new PixelShaderEffect(shaderCodeBytes)
            {
                Source1 = source,
                Properties =
                {
                    ["threshold"] = Threshold,
                    ["sourceColor"] = new Vector3( SourceColor.R/255, SourceColor.G / 255, SourceColor.B / 255),
                    ["replaceColor"] = new Vector3(TargetColor.R / 255, TargetColor.G / 255, TargetColor.B / 255)
                }
            };
            return effect;
        }

        
    }
}
